<?php

function price_format($price, $abbr = false){
	$sf_user = sfContext::getInstance()->getUser();
	$culture = $sf_user->getCulture();
	$cur = $sf_user->getCurrency();
	
	$abbr = $abbr ? $abbr : $cur->getAbbr();
	
	
	$numberFormat = new sfNumberFormat($culture);
	
	$ar = sfNumberFormatInfo::getCurrencyInstance($culture)->getPattern();

	 // //#,##0.00 ¤

	if($abbr != 'EUR' && $abbr != 'USD'){
		$ar['positive'] = '¤ #0';
		
		$pattern = str_replace('¤', '', $ar['positive']);
		
		$pattern = trim(str_replace('¤', '', $ar['positive'])) . ' ' .$abbr;
	}else{
		$pattern = $ar['positive'];
	}
	$pattern = '¤ '.str_replace('¤', '', $pattern);

	return $numberFormat->format(get_currency_factor($abbr) * $price, $pattern, $abbr);
}

function price_convert($q, $toCurrency, $fromCurrency = Currency::DEFAULT_CURRENCY, $culture = null){
	$r = Currency::convert($q, $toCurrency, $fromCurrency);
	

	
	return $r;
}

function get_currency_factor($abbr){

	$cur = false;
	try{
		$cur = sfContext::getInstance()->get($abbr);
	}catch(sfException $e){
		$cur = Doctrine::getTable('Currency')->findOneByAbbr($abbr);
		sfContext::getInstance()->set($abbr,$cur);
	}

	if($cur){
		return $cur['factor'];
	}else{
		return 1;
	}
}

